package com.syt.suan.system.controller;

import com.syt.suan.common.core.domain.R;
import com.syt.suan.system.domain.dtos.UserAddDTO;
import com.syt.suan.system.domain.dtos.UserLoginDTO;
import com.syt.suan.system.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 管理端用户表 前端控制器
 * </p>
 *
 * @author chen
 * @since 2024-12-04
 */
@RestController
@RequestMapping("/system/user")
@Tag(name = "管理员用户API")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/login")
    @Operation(summary = "管理员登录", description = "根据提供的账号密码进行管理员登录")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3102", description = "用户不存在")
    @ApiResponse(responseCode = "3103", description = "用户名或密码错误")
    public R<?> login(@Validated @RequestBody UserLoginDTO loginDTO) {
        String userAccount = loginDTO.getUserAccount();
        String password = loginDTO.getPassword();

        return userService.login(userAccount, password);
    }

    @PostMapping("/add")
    @Operation(summary = "管理员新增用户", description = "根据提供的信息进行用户新建")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    public R<?> add(@Validated @RequestBody UserAddDTO addDTO) {
        String userAccount = addDTO.getUserAccount();
        String password = addDTO.getPassword();
        String nickName = addDTO.getNickName();
        Long createBy = addDTO.getCreateBy();

        return userService.add(userAccount, password, nickName, createBy);
    }
}
